<template>
  <Modal v-model="visible"
         :title="dataForm.jobId ? '编辑' : '新增'"
         :closable="false"
         :mask-closable="false">
    <Form ref="dataForm" :model="dataForm" :rules="ruleValidate" :label-width="120">
      <FormItem label="bean名称" prop="beanName">
        <Input v-model="dataForm.beanName" placeholder="输入spring bean名称" clearable></Input>
      </FormItem>
      <FormItem label="方法名" prop="methodName">
        <Input v-model="dataForm.methodName" placeholder="输入方法名" clearable></Input>
      </FormItem>
      <FormItem label="参数" prop="params">
        <Input v-model="dataForm.params" placeholder="输入参数" clearable></Input>
      </FormItem>
      <FormItem label="cron表达式" prop="cronExpression">
        <Input v-model="dataForm.cronExpression" placeholder="输入cron表达式,例如：0 0/30 * * * ?" clearable></Input>
      </FormItem>
      <FormItem label="备注" prop="remark">
        <Input v-model="dataForm.remark" placeholder="输入备注" clearable></Input>
      </FormItem>
    </Form>
    <div slot="footer">
      <Button type="text" @click="handleCancel">取消</Button>
      <Button type="primary" :loading="showLoading" @click="handleSubmit">保存</Button>
    </div>
  </Modal>
</template>
<script>
export default {
  data () {
    return {
      visible: false,
      showLoading: false,
      dataForm: {
        jobId: 0,
        beanName: '',
        methodName: '',
        params: '',
        cronExpression: '',
        status: 0,
        remark: '',
        createTime: ''
      },
      ruleValidate: {
        beanName: [
          {required: true, message: 'spring bean名称不能为空', trigger: 'blur'}
        ],
        methodName: [
          {required: true, message: '方法名不能为空', trigger: 'blur'}
        ],
        cronExpression: [
          {required: true, message: 'cron表达式不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init (id) {
      // add or update
      this.$refs['dataForm'].resetFields();
      this.visible = true;
      this.dataForm.jobId = id || 0;
      if (this.dataForm.jobId) {
        // 根据主键获取详情
        let url = '/sys/schedule/info/' + this.dataForm.jobId;
        this.$http({
          url: this.$http.adornUrl(url),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.beanName = data.schedule.beanName;
            this.dataForm.methodName = data.schedule.methodName;
            this.dataForm.params = data.schedule.params;
            this.dataForm.cronExpression = data.schedule.cronExpression;
            this.dataForm.status = data.schedule.status;
            this.dataForm.remark = data.schedule.remark;
            this.dataForm.createTime = data.schedule.createTime;
          }
        });
      }
    },
    handleCancel () {
      this.visible = false;
      this.disablePassword = true;
      this.showLoading = false;
    },
    handleSubmit () { // 表单提交
      let url = '/sys/schedule/' + (this.dataForm.jobId ? 'update' : 'save');
      let param = {
        'jobId': this.dataForm.jobId || undefined,
        'beanName': this.dataForm.beanName,
        'methodName': this.dataForm.methodName,
        'params': this.dataForm.params,
        'cronExpression': this.dataForm.cronExpression,
        'status': this.dataForm.status,
        'remark': this.dataForm.remark// ,
        // 'createTime': this.dataForm.createTime
      };
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.showLoading = true;
          this.$http({
            url: this.$http.adornUrl(url),
            method: 'post',
            data: this.$http.adornData(param)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$Message.success({
                content: '操作成功',
                duration: 2,
                onClose: () => {
                  this.handleCancel();
                  this.$emit('refreshDataList')
                }
              });
            } else {
              this.$Message.error(data.msg);
              this.showLoading = false;
            }
          })
        }
      })
    }
  }
}
</script>
